package jp.co.sharp.android.parents.kidsguard.common;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;

public class DatePickerDlg extends Dialog {

    private NumberPicker mDlgYearSpinner;
    private NumberPicker mDlgMonthSpinner;
    private NumberPicker mDlgDaySpinner;
    private Context mContext;
    private int mYear, mMonth, mDay;
    private OnDateSetListener mOnDateSetListener;
    private DatePickerDlg mDatePickerDlg;
    private Integer pYear, pMonth, pDay;
    private List<Integer> mMls31DayMonth = new ArrayList<>(Arrays.asList(1, 3, 5, 7, 8, 10, 12));
    private List<Integer> mMls30DayMonth = new ArrayList<>(Arrays.asList(4, 6, 9, 11));
    private List<Integer> mMls29DayMonth = new ArrayList<>(Arrays.asList(2));

    public DatePickerDlg(@NonNull Context context, Integer pYear, Integer pMonth, Integer pDay) {
        super(context);
        mContext = context;
        mDatePickerDlg = this;
        this.pYear = pYear;
        this.pMonth = pMonth;
        this.pDay = pDay;
    }

    public DatePickerDlg(@NonNull Context context, int themeResId, Integer pYear, Integer pMonth, Integer pDay) {
        super(context, themeResId);
        mContext = context;
        mDatePickerDlg = this;
        this.pYear = pYear;
        this.pMonth = pMonth;
        this.pDay = pDay;
    }

    protected DatePickerDlg(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener, Integer pYear, Integer pMonth, Integer pDay) {
        super(context, cancelable, cancelListener);
        mContext = context;
        mDatePickerDlg = this;
        this.pYear = pYear;
        this.pMonth = pMonth;
        this.pDay = pDay;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dlg_date_picker);

        initDlg();
    }

    private void initDlg() {
        mDlgYearSpinner = (NumberPicker) this.findViewById(R.id.dlgYear);
        List<String> pDlgYear = new ArrayList();
        int fistYear = 1970;
        for (int i = 0; i <= 60; i++) {
            pDlgYear.add(String.valueOf(fistYear + i));
        }
        String[] strDlgYear = new String[pDlgYear.size()];
        mDlgYearSpinner.setDisplayedValues(pDlgYear.toArray(strDlgYear));
        mDlgYearSpinner.setOnValueChangedListener(mOnYearChangedListener);
        mDlgYearSpinner.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        mDlgYearSpinner.setMinValue(fistYear);
        mDlgYearSpinner.setMaxValue(Integer.valueOf(pDlgYear.get(strDlgYear.length - 1)));
        mDlgYearSpinner.setWrapSelectorWheel(false);
        setDividerColor(mDlgYearSpinner);
        if (null != pYear) {
            mDlgYearSpinner.setValue(pYear);
        }

        DecimalFormat decimalFormat = new DecimalFormat("00");
        mDlgMonthSpinner = (NumberPicker) this.findViewById(R.id.dlgMonth);
        List<String> pDlgMonth = new ArrayList();
        for (int i = 1; i <= 12; i++) {
            pDlgMonth.add(decimalFormat.format(i));
        }
        String[] strDlgMonth = new String[pDlgMonth.size()];
        mDlgMonthSpinner.setDisplayedValues(pDlgMonth.toArray(strDlgMonth));
        mDlgMonthSpinner.setMinValue(1);
        mDlgMonthSpinner.setMaxValue(strDlgMonth.length);
        mDlgMonthSpinner.setOnValueChangedListener(mOnMonthChangedListener);
        mDlgMonthSpinner.setWrapSelectorWheel(false);
        setDividerColor(mDlgMonthSpinner);
        if (null != pMonth) {
            mDlgMonthSpinner.setValue(pMonth);
        }

        mDlgDaySpinner = (NumberPicker) this.findViewById(R.id.dlgDay);
        List<String> pDlgDay = new ArrayList();
        for (int i =1; i <= 31; i++) {
            pDlgDay.add(decimalFormat.format(i));
        }
        String[] strDlgDay = new String[pDlgDay.size()];
        mDlgDaySpinner.setDisplayedValues(pDlgDay.toArray(strDlgDay));
        mDlgDaySpinner.setMinValue(1);
        mDlgDaySpinner.setMaxValue(strDlgDay.length);
        mDlgDaySpinner.setOnValueChangedListener(mOnDayChangedListener);
        mDlgDaySpinner.setWrapSelectorWheel(false);
        setDividerColor(mDlgDaySpinner);
        if (null != pDay) {
            mDlgDaySpinner.setValue(pDay);
        }

        Button buttonOK = (Button)this.findViewById(R.id.yes);
        buttonOK.setOnClickListener(new ListenerOK());

        Button buttonCancel = (Button)this.findViewById(R.id.cancel);
        buttonCancel.setOnClickListener(new ListenerCancel());
    }

    /**
     *
     * 控件监听器
     */
    private NumberPicker.OnValueChangeListener mOnYearChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mYear = newVal;
            cutoverDay();
        }
    };

    private NumberPicker.OnValueChangeListener mOnMonthChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mMonth = newVal;
            cutoverDay();
        }
    };

    private NumberPicker.OnValueChangeListener mOnDayChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            mDay = newVal;
        }
    };

    private void cutoverDay() {
        if (mMls31DayMonth.contains(mMonth)) {
            mDlgDaySpinner.setMaxValue(31);
        } else if (mMls30DayMonth.contains(mMonth)) {
            mDlgDaySpinner.setMaxValue(30);
        } else if (mMls29DayMonth.contains(mMonth)) {
            int day = 28;
            if (mYear % 4 == 0) {
                day = 29;
            }
            mDlgDaySpinner.setMaxValue(day);
        }
    }

    /**
     *
     */
    public interface OnDateSetListener {
        void onDateSet(int pYear, int pMonth, int pDay);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void setOnDateSetListener(OnDateSetListener callBack) {
        mOnDateSetListener = callBack;
    }

    class ListenerOK implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (mOnDateSetListener != null) {
                mOnDateSetListener.onDateSet(mDlgYearSpinner.getValue(), mDlgMonthSpinner.getValue(), mDlgDaySpinner.getValue());
                mDatePickerDlg.dismiss();
            }
        }
    }

    class ListenerCancel implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (mOnDateSetListener != null) {
                mDatePickerDlg.dismiss();
            }
        }
    }

    /**
     * 设置picker分割线的颜色
     */
    private void setDividerColor(NumberPicker picker) {
        Field field = null;
        try {
            field = NumberPicker.class.getDeclaredField("mSelectionDivider");
            if (field != null) {
                field.setAccessible(true);
                field.set(picker, new ColorDrawable(ContextCompat.getColor(mContext, R.color.colorDlgTimePicker)));
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

}
